<?php
require_once(dirname(__FILE__) . '/inc/config.inc.php');
IsModelPriv('goodsorder');
require_once "function.php";
/*
进度：
0 已经撤单
1 报修成功
2 客服已经联系
3 系统已经派单
4 工程师接单
5 维修完成
6 已经评价
*/
$op_uid = $_SESSION['uid'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<title>发布任务</title>
	<link href="templates/style/admin.css" rel="stylesheet" type="text/css"/>
	<script type="text/javascript" src="templates/js/jquery.min.js"></script>
	<script type="text/javascript" src="layer/layer.js"></script>
	<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
	<link href="layui/css/layui.css" rel="stylesheet" type="text/css" media="all"/>
	<script src="layui/layui.js"></script>
	<script language="javascript" type="text/javascript" src="DatePicker/WdatePicker.js"></script>

</head>
<style>
    #test-upload-more-list {
        display: block;
        width: 100%;
        height: auto;
        overflow: auto;
    }

    #test-upload-more-list div {
        float: left;
        width: 130px;
        height: 180px;
        margin: 0 10px 10px 0
    }

    #test-upload-more-list img {
        width: 130px;
        height: 130px;
    }

    .w20 {
        display: inline-block;
        margin-left: 5px;
        width: 30px !important;
    }

    .w100 {
        display: inline-block;
        margin-left: 5px;
        width: 100px !important;
    }

    .w200 {
        display: inline-block;
        margin-left: 5px;
        width: 200px !important;
    }

    .homepage-bottom .layui-card-body img {
        width: 64px;
        height: 64px;
        border-radius: 50%;
        margin-top: 10px;
    }

    .homepage-bottom .layui-card-body .meida-left {
        display: table-cell;
        padding-right: 10px;
    }

    .homepage-bottom .layui-card-body .meida-right {
        display: table-cell;
        vertical-align: top;
    }

    .homepage-bottom .layui-card-body a:hover {
        background: #F2F2F2;
    }

    .homepage-bottom .layui-card-body {
        background: #fff;
    }

    .layui-card-header .panel-title .layui-icon {
        position: relative;
        left: 0;
        right: 0px;
    }
</style>
<body style="width:890px;">
<div class="layui-fluid">
	<div class="layui-card">
		<h1 class="tit" style="height: 50px; line-height: 50px;">发布任务</h1>
		<div class="layui-card-header">
			<div class="order_top1">发布人：<?php echo GetAuthor(); ?></div>
			<div class="order_top1 t_fr">创建日期：<?php echo date("Y年m月d日", time()); ?></div>
		</div>
		<div class="layui-card-body layui-layout-body" style=" padding: 15px; ">
			<div class="layui-form layui-col-md12" lay-filter="component-form-group" id="form1">
				<div class="layui-form-item">
					<div class="layui-col-sm6"><label class="layui-form-label layui-bg-blue">服务类型<span
								class="red">*</span></label>
						<div id="ordertypebox1" class="layui-input-inline">
							<select name="task_type" id="task_type" lay-filter="task_type">
								<option value="0">请选择</option>
                                <?php
                                $dosql->Execute("SELECT * FROM `#@__tasktype` WHERE `checkinfo`='1' and is_delete=0 order by orderid ASC", 10);
                                while ($row = $dosql->GetArray(10)) {
                                    echo '<option value="' . $row['id'] . '" >' . $row['classname'] . '</option>';

                                }
                                ?>
							</select>
						</div>
					</div>
					<div class="layui-col-sm6"><label class="layui-form-label layui-bg-green">项目名称<span
								class="red">*</span></label>
						<div id="ordertypebox1" class="layui-input-inline">
							<select name="task" id="task" lay-filter="task">
								<option value="0">请选择</option>

							</select>
						</div>
					</div>
				</div>
				<div class="layui-form-item">
					<div class="layui-col-sm6"><label class="layui-form-label">客户<span class="red">*</span></label>
						<div class="layui-input-inline">
							<select name="crmid" id="crmid" lay-filter="crmid" ay-verify="required" lay-search="">
								<option value="0">请选择</option>
                                <?php
                                $dosql->Execute("SELECT * FROM `#@__crm` WHERE `checkinfo`=1 and is_delete=0 order by id ASC", 10);
                                while ($row = $dosql->GetArray(10)) {
                                    echo '<option value="' . $row['id'] . '" >' . $row['school'] . '</option>';

                                }
                                ?>
							</select>
							<input type="hidden" id="school" name="school" value=""/>
						</div>
					</div>
					<div class="layui-col-sm6"><label class="layui-form-label">联系人</label>
						<div id="lianxirenbox" class="layui-input-inline">
							<input type="text" name="truename" id="truename"
								   autocomplete="off" id="contact" placeholder="请输入报修人"
								   class="layui-input"></div>
					</div>

				</div>
				<div class="layui-form-item">
					<div class="layui-col-sm6"><label class="layui-form-label">联系方式</label>
						<div class="layui-input-inline">

							<input type="text" name="mobile"
								   autocomplete="off" id="mobile" placeholder="请输入报修人联系方式"
								   class="layui-input"></div>
					</div>
					<div class="layui-col-sm6"><label class="layui-form-label">地址</label>
						<div class="layui-input-inline">

							<input type="text" name="address"
								   autocomplete="off" id="address" placeholder="请输入报修人地址"
								   class="layui-input"></div>
					</div>
				</div>
				<div class="layui-form-item layui-row">
					<div class="layui-col-sm6"><label class="layui-form-label">开始时间<span class="red">*</span></label>
						<div id="regionbox" class="layui-input-inline">
							<input class="layui-input " name="beginTime" id="beginTime" type="text"
								   onClick="WdatePicker({el:this,dateFmt:'yyyy-MM-dd HH:mm:ss'})"
								   value="<?php echo date("Y-m-d H:i:s", time()) ?>">
						</div>
					</div>
					<div class="layui-col-sm6"><label class="layui-form-label">结束时间<span class="red">*</span></label>
						<div id="regionbox" class="layui-input-inline">
							<input class="layui-input" name="endTime" id="endTime" type="text"
								   onClick="WdatePicker({el:this,dateFmt:'yyyy-MM-dd HH:mm:ss'})"
								   value="<?php echo date("Y-m-d H:i:s", time() + 14400) ?>">
						</div>
					</div>


				</div>

				<div class="layui-form-item"><label class="layui-form-label">故障描述<span class="red">*</span></label>
					<div class="layui-input-block"><textarea name="content" lay-verify="required"
															 placeholder="请输入故障现象描述" class="layui-textarea"></textarea>
					</div>

				</div>

				<div class="layui-form-item">
					<label class="layui-form-label ">图片<span
							style="color: red">*</span></label>
					<div class="layui-input-block" style="margin-left: 120px">
						<a type="button" class="layui-btn layui-btn-danger"
						   id="test-upload-more">多图片上传
						</a>
						<blockquote class="layui-elem-quote layui-quote-nm"
									style="margin-top: 10px;">
							预览图：
							<div class="layui-upload-list"
								 id="test-upload-more-list"></div>
						</blockquote>
					</div>

				</div>

				<div class="layui-form-item">
					<label class="layui-form-label">模式<span class="red">*</span></label>
					<div class="layui-input-block">
						<input name="qiang" type="radio" value="q" title="抢单" lay-filter="moshi" checked=""/>
						<input name="qiang" type="radio" value="p" title="派单" lay-filter="moshi"/>
					</div>
					<input type="hidden" name="mode" id="mode" value="q"/>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">工程师<span class="red">*</span></label>
					<div class="layui-input-block">
						<blockquote class="layui-elem-quote" id="gongchengshi" style="display: none;">

							公司本部：<br/>
                            <?php
                            $dosql->Execute("SELECT * FROM  `#@__user`    WHERE level='3'   ORDER BY id asc");
                            while ($row3 = $dosql->GetArray()) {
                                echo '<input  type="checkbox" lay-skin="primary" name="manageid" title="' . $row3['truename'] . '" value="' . $row3['id'] . '"/>';
                            }
                            ?>

						</blockquote>

						<blockquote class="layui-elem-quote" id="gongchengshi2" style="display: none;">
							授权服务站：<br/>
                            <?php
                            $dosql->Execute("SELECT * FROM  `#@__user`    WHERE level='4'   ORDER BY id asc");
                            while ($row3 = $dosql->GetArray()) {
                                echo '<input  type="checkbox" lay-skin="primary" name="manageid" title="' . $row3['truename'] . '" value="' . $row3['id'] . '"/>';
                            }
                            ?>

						</blockquote>


						<input type="hidden" name="manageids" id="manageids" value=""/>
					</div>
				</div>

				<div class="layui-form-item" id="sub_box">
					<div class="layui-input-inline">
						<button class="layui-btn" lay-submit="" lay-filter="layuiadmin-order-submit">立即提交</button>
						<button class="layui-btn layui-btn-primary" data-type="reset">重置</button>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>

<script>
	layui.use(['form', 'laydate', 'table', 'layedit', 'upload'], function () {
		var $ = layui.$,
			element = layui.element,
			layer = layui.layer,
			view = layui.view,
			table = layui.table,
			laytpl = layui.laytpl,
			laydate = layui.laydate,
			layedit = layui.layedit,
			upload = layui.upload,
			form = layui.form;


		//普通图片上传
		var uploadInst = upload.render({
			elem: '#test-upload-normal'
			//, url: '/upload/'
			, url: '../admin/upload.php' //接口url
			, before: function (obj) {
				//预读本地文件示例，不支持ie8
				obj.preview(function (index, file, result) {
					$('#test-upload-normal-img').attr('src', result); //图片链接（base64）
				});
			}
			, done: function (res) {
				//如果上传失败
				if (res.code > 0) {
					return layer.msg('上传失败');
				}
				//上传成功
				if (res.code == 0) {
					layer.msg("上传成功");
					$("#picurl").val(res.image);
				}
			}
			, error: function () {
				//演示失败状态，并实现重传
				var demoText = $('#test-upload-demoText');
				demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
				demoText.find('.demo-reload').on('click', function () {
					uploadInst.upload();
				});
			}
		});
		layedit.set({
			uploadImage: {
				url: '../core/upload.php', //接口url
				type: 'post', //默认post
			}
		});
		form.render(null, 'component-form-group');
		//监听提交
		form.on('submit(layuiadmin-order-submit)', function (data) {

			var beginTime = $("#beginTime").val();
			var endTime = $("#endTime").val();
			var d1 = new Date(beginTime.replace(/\-/g, "\/"));
			var d2 = new Date(endTime.replace(/\-/g, "\/"));
			console.log(d1);
			if (beginTime != "" && endTime != "" && d1 >= d2) {
				layer.msg("开始时间不能大于结束时间！");
				return false;
			}


			var mode = $('input[name="qiang"]:checked').val();
			if (mode == 'p') {
				// 是否选择工程师
				var checkids = [];
				var ids = "";
				$("input[name='manageid']:checked").each(function (i) {
					checkids[i] = $(this).val();
				});

				if (checkids.length == 0) {
					layer.msg("请选择工程师！");
					return false;
				}
				ids = checkids.join(",");
				data.field.manageids = ids;
			}


			var field = data.field; //获取提交的字段
			//提交 Ajax 成功后，关闭当前弹层并重载表格
			var picarr = [];
			$('input[name="picarr"]').each(function () {
				console.log($(this).val());
				picarr.push($(this).val());
			});
			field.picarr_arr = picarr;
			console.log(picarr)
			$.ajax({
				url: "json/order/order_do.php",
				data: {
					"field": field,
					"action": 'add'

				},
				type: "POST",
				dataType: "json",
				success: function (res) {
					console.log(res);
					if (res.code == 0) {
						console.log("返回成功");
						var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
						parent.layer.close(index); //再执行关闭

					} else {
						layer.msg(res.msg);
					}

				}
			});
		});

		//获取分类
		form.on('select(task_type)', function (data) {
			console.log("点击了 任务分类");
			var typeid = data.value;
			$.ajax({
				type: 'post',
				url: "json/task/getTypeSub.php?typeid=" + typeid,
				//data: {areaId:areaId},
				dataType: 'json',
				success: function (res) {
					console.log(res.data);
					$("#task").html("");
					$("#task").html(res.data);
					form.render('select');
					$("#task").get(0).selectedIndex = 0;
				}

			});
		});


		//选择客户自动带出客户的地址信息
		form.on('select(crmid)', function (data) {
			console.log(data.value); //得到被选中的值
			_crmid = data.value;
			$.ajax({
				url: "json/crm/getCrmDetail.php",
				data: {
					"id": _crmid,
				},
				type: "POST",
				dataType: "json",
				success: function (res) {
					console.log(res);
					if (res.code == 0) {
						$("#truename").val(res.data.truename);
						$("#mobile").val(res.data.mobile);
						$("#address").val(res.data.address);
						$("#school").val(res.data.school);
						form.render(null, 'component-form-group');
					}

				}
			});
		});

		form.on('radio(moshi)', function (data) {
			_mode = data.value;
			$("#mode").val(_mode);
			if (_mode == 'p') {
				$("#gongchengshi").show();
				$("#gongchengshi2").show();
			}
			if (_mode == 'q') {
				$("#gongchengshi").hide();
				$("#gongchengshi2").hide();
			}
		});
		//上传组图
		var filepaths = "";
		//多图片上传
		var upins = upload.render({
			elem: '#test-upload-more'
			, url: 'https://zmdtc.oss-cn-beijing.aliyuncs.com' //接口url
			, multiple: true
			, data: {
				key: function () {
					let timeis = Date.parse(new Date());
					let letter = Math.random().toString(36).substr(2);
					return "uploads/" + timeis + letter + ".jpg";
				},
				'policy': '',
				'OSSAccessKeyId': '',
				'success_action_status': "200",
				'signature': ''
			}
			, before: function (obj) {
				console.log(obj);
				$.ajax({
					url: 'oss/get.php',
					data: {},
					async: false,
					dataType: "json",
					success: function (res) {
						console.log(res.policy);
						upins.config.data.policy = res.policy;
						upins.config.data.OSSAccessKeyId = res.accessid;
						upins.config.data.signature = res.signature;
						upins.config.data.callback = res.callback;
						upins.config.data.success_action_status = '200'
					}

				});
			}
			, done: function (res, index) {
				//上传完毕
				if (res.status == 'ok') {
					console.log(res)
					//上传完毕
					$('#test-upload-more-list').append('<div class="layui-upload-img" id="' + index + '"><img src="' + res.data.src + '"  class="layui-upload-img"><a  class="layui-btn layui-btn-sm layui-btn-normal" href="javascript:void(0);" onclick=ClearPicArr("' + index + '")>删除</a><input type="hidden" name="picarr" value="' + res.data.filename + '" ></div>')
					layui.form.render();
				}
			}
		});
		layui.form.render();


	});

	//删除图片
	function ClearPicArr(id) {
		console.log(id);
		document.getElementById(id).innerHTML = "";
		document.getElementById(id).style.display = "none";
	}
</script>
</body>
</html>
